package com.zhengtoudai.ui.fragment;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.zhengtoudai.R;
import com.zhengtoudai.base.BaseFragment;
import com.zhengtoudai.http.HttpApi;
import com.zhengtoudai.view.ProgressWebView;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.zhengtoudai.R.id.webView;


public class WebViewFragment extends BaseFragment {

    @Bind(webView)
    ProgressWebView mWebView;
    private String mUrl;

    public WebViewFragment() {
        // Required empty public constructor
    }


    public static WebViewFragment newInstance(String mUrl) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle args = new Bundle();
        args.putString("url", mUrl);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mUrl = getArguments().getString("url");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_web_view, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        mWebView. setWebViewClient(new WebViewClient() {

            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //handler.cancel(); // Android默认的处理方式
                handler.proceed();  // 接受所有网站的证书  授权证书()
                //handleMessage(Message msg); // 进行其他处理
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.e("current_url",url);
            }
        });
        mWebView.loadUrl(mUrl, HttpApi.getHeader());
        Log.e("url",mUrl);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
